/**
 * @ClassName LC23
 * @Author ykx
 * @Date 2024/3/6 23:10
 * @Version 1.0
 */
public class LC23 {
    public static void main(String[] args) {

    }

    public ListNode mergeKLists(ListNode[] lists) {
        int length = lists.length;

        ListNode dummyHead = new ListNode(0);
        ListNode tail = dummyHead;

        while (true) {
            ListNode minNode = null;
            int min = -1;
            for (int i = 0; i < length; i++) {
                if (lists[i] == null) {
                    continue;
                }
                if (minNode == null || lists[i].val < minNode.val) {
                    minNode = lists[i];
                    min = i;
                }
            }
            if (min == -1) {
                break;
            }
            tail.next = minNode;
            tail = tail.next;
            lists[min] = lists[min].next;
        }
        return dummyHead.next;
    }

}
